
package com.syh.niceweibo.common;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.syh.niceweibo.common.infos.HotWeiboInfo;
import com.syh.niceweibo.common.infos.UserInfo;
import com.syh.niceweibo.common.infos.WeiboInfo;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboException;

public class WeiboImpl {
    private final static String TAG = "WeiboImpl";

    public static String[] getUserInfo(String result) {
        String[] userInfo = new String[2];
        try {
            JSONObject jResult = new JSONObject(result);
            userInfo[0] = jResult.getString("screen_name");
            userInfo[1] = jResult.getString("profile_image_url");
        } catch (JSONException e) {
            e.printStackTrace();
            if(LogUtils.DEBUG)LogUtils.e(TAG, "GetUserInfoTask JSONException:" + e.toString());
        }
        return userInfo;
    }
    
    /**
     * 获取精选微博
     * @param context
     * @param type 1：娱乐、2：搞笑、3：美女、4：视频、5：星座、6：各种萌、7：时尚、8：名车、9：美食、10：音乐
     * @param count  单页返回的记录条数，默认为20。
     * @param page  返回结果的页码，默认为1。
     * @return
     */
    public static ArrayList<HotWeiboInfo> getHotWeiboInfo(Context context,int type,int count,int page){
    	
    	try {
    		int is_pic=1;
			String result = Weibo.getInstance().getHotTimeline(context, Utils.getAccessToken(context),type,is_pic, count,1000);
			if(LogUtils.DEBUG)LogUtils.d(TAG,"getClassWeibo result:"+result.substring(result.length()-200,result.length()-1));
			if(LogUtils.DEBUG)LogUtils.d(TAG,"getClassWeibo result:"+result);
			return changeJsonToHotWeiboInfoList(result);
    	} catch (WeiboException e) {
			e.printStackTrace();
			if(LogUtils.DEBUG)LogUtils.e(TAG,"getClassWeibo e:"+e.toString());
			return null;
		}
    	
    }
    
    private static ArrayList<HotWeiboInfo> changeJsonToHotWeiboInfoList(String json){
    	try {
    		JSONObject root=new JSONObject(json);
    		int total_count=root.getInt("total_number");
    		if(LogUtils.DEBUG)LogUtils.e(TAG,"changeJsonToHotWeiboInfoList total_count:"+total_count);
			JSONArray jsonArray=new JSONObject(json).getJSONArray("statuses");// JSONArray(json);
			ArrayList<HotWeiboInfo> result=new ArrayList<HotWeiboInfo>();
			WeiboInfo weibo;
			UserInfo user;
			HotWeiboInfo HotWeiboInfo;
			for(int i=0;i<jsonArray.length();i++){
				JSONObject object=jsonArray.getJSONObject(i).getJSONObject("status");
				if(LogUtils.DEBUG)LogUtils.d(TAG,"HotWeiboInfo i:"+i+" json:"+object.toString());
				weibo=new WeiboInfo();
				weibo.weiboId=object.getLong("id");
				weibo.content=object.getString("text");
				weibo.created_at=Utils.formatTime(object.getString("created_at"));
				try {
					weibo.bmiddle_pic=object.getString("bmiddle_pic");
					weibo.original_pic=object.getString("original_pic");
				} catch (Exception e) {
					e.printStackTrace();
				}

				weibo.transmitCount=object.getInt("reposts_count");
				if(LogUtils.DEBUG)LogUtils.d(TAG,"get original_pic i:"+i+":"+weibo.original_pic);
				
				JSONObject userObject=object.getJSONObject("user");
				user=new UserInfo();
				user.uid=userObject.getLong("id");
				user.screen_name=userObject.getString("screen_name");
				user.profile_image_url=userObject.getString("profile_image_url");
				
				HotWeiboInfo=new HotWeiboInfo();
				HotWeiboInfo.weiboInfo=weibo;
				HotWeiboInfo.userInfo=user;
				if(weibo.original_pic!=null && weibo.bmiddle_pic!=null){
					result.add(HotWeiboInfo);
				}
			}
			if(LogUtils.DEBUG)LogUtils.d(TAG,"changeJsonToHotWeiboInfoList size:"+result.size());
			return result;
		} catch (JSONException e) {
			e.printStackTrace();
			if(LogUtils.DEBUG)LogUtils.e(TAG,"changeJsonToHotWeiboInfoList e:"+e.toString());
		}
    	
    	return null;
    }
}
